package com.example.springbootdemo.utils;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.interactive.form.PDNonTerminalField;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ReadFileUtil {

    /*public static String[] readFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        if (file.getName().endsWith(".pdf")){
           return readPdf(inputStream);
        }
        return readDoc(inputStream);
    }


    public static String[] readPdf(InputStream inputStream) throws IOException {
        try(PDDocument document = PDDocument.load(inputStream)){
            // 创建PDFTextStripper对象
            PDFTextStripper stripper = new PDFTextStripper();
//            String s = stripper.getText(document).replaceAll("\\r?\\n", "");
            String s = stripper.getText(document);
            String[] split = s.split("\\r?\\n");
//            System.out.println(split.length);
            return split;
        }
    }



    public static String[] readDoc(InputStream inputStream) throws IOException {
        XWPFDocument document = new XWPFDocument(inputStream);
//        StringBuilder builder = new StringBuilder();
        ArrayList<String> stringArrayList = new ArrayList<>();
        // 获取文档中的所有段落和表格
        List<IBodyElement> elements = document.getBodyElements();

        // 遍历所有段落和表格
        for (IBodyElement element : elements) {
            // 判断元素类型并进行相应处理
            if (element instanceof XWPFParagraph) {
                XWPFParagraph paragraph = (XWPFParagraph) element;
                String paragraphText = paragraph.getText();
//                builder.append(paragraphText);
                stringArrayList.add(paragraphText);
            } else if (element instanceof XWPFTable) {
                XWPFTable table = (XWPFTable) element;

                // 遍历表格中的行
                List<XWPFTableRow> rows = table.getRows();
                for (XWPFTableRow row : rows) {
                    // 遍历行中的单元格
                    List<XWPFTableCell> cells = row.getTableCells();
                    for (XWPFTableCell cell : cells) {
                        String cellText = cell.getText();
//                        builder.append(cellText);
                        stringArrayList.add(cellText);
                    }
                }
            }
        }
//        return builder.toString().replaceAll("\\r?\\n", "");
//        return builder.toString().replaceAll("(\\r?\\n){2,}", "");
        String[] stringArray = stringArrayList.toArray(new String[0]);
        return stringArray;
//        String cleanedText = builder.toString();
        // Replace consecutive newlines with no other characters with a single newline

//        return cleanedText;
    }
*/
    public static String readFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        if (file.getName().endsWith(".pdf")){
            return readPdf(inputStream);
        }
        return readDoc(inputStream);
    }


    public static String readPdf(InputStream inputStream) throws IOException {
        try(PDDocument document = PDDocument.load(inputStream)){
            // 创建PDFTextStripper对象
            PDFTextStripper stripper = new PDFTextStripper();
            String s = stripper.getText(document).replaceAll("\\r?\\n", "");
            return s;
        }
    }



    public static String readDoc(InputStream inputStream) throws IOException {
        XWPFDocument document = new XWPFDocument(inputStream);
        StringBuilder builder = new StringBuilder();
//        ArrayList<String> stringArrayList = new ArrayList<>();
        // 获取文档中的所有段落和表格
        List<IBodyElement> elements = document.getBodyElements();

        // 遍历所有段落和表格
        for (IBodyElement element : elements) {
            // 判断元素类型并进行相应处理
            if (element instanceof XWPFParagraph) {
                XWPFParagraph paragraph = (XWPFParagraph) element;
                String paragraphText = paragraph.getText();
                builder.append(paragraphText);
//                stringArrayList.add(paragraphText);
            } else if (element instanceof XWPFTable) {
                XWPFTable table = (XWPFTable) element;

                // 遍历表格中的行
                List<XWPFTableRow> rows = table.getRows();
                for (XWPFTableRow row : rows) {
                    // 遍历行中的单元格
                    List<XWPFTableCell> cells = row.getTableCells();
                    for (XWPFTableCell cell : cells) {
                        String cellText = cell.getText();
                        builder.append(cellText);
//                        stringArrayList.add(cellText);
                    }
                }
            }
        }
        return builder.toString().replaceAll("\\r?\\n", "");
//        return builder.toString().replaceAll("(\\r?\\n){2,}", "");
//        String[] stringArray = stringArrayList.toArray(new String[0]);
//        return stringArray;
//        String cleanedText = builder.toString();
        // Replace consecutive newlines with no other characters with a single newline

//        return cleanedText;
    }
}
